package com.wtwd.device.model.dto;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wtwd.device.model.entity.Step;
import com.wtwd.device.util.DateUtils;
import io.swagger.annotations.ApiModelProperty;

import java.util.List;

/**
 * Created by Roc on 2021/8/6.
 * <p>
 * 步数记录
 * <p>
 * 以一天统计出来的记录
 */
public class SportRecord {
    /**
     * 设备ID
     */
    @ApiModelProperty(value = "设备ID", required = true)
    private long deviceId;
    /**
     * 时间，格式：yyyy-MM-dd
     */
    @ApiModelProperty(value = "时间", required = true)
    private String time;
    /**
     * 总步数
     */
    @ApiModelProperty(value = "总步数", required = true)
    private int totalSteps;
    /**
     * 总运动时间：单位秒
     */
    @ApiModelProperty(value = "总时间(s)", required = true)
    private int totalTime;
    /**
     * 总运动距离：单位米
     */
    @ApiModelProperty(value = "总距离(m)", required = true)
    private int totalDistance;
    /**
     * 总运动卡路里
     */
    @ApiModelProperty(value = "消耗卡路里(kcal)", required = true)
    private int totalCalories;
    /**
     * 记录详情集合
     */
    @ApiModelProperty(value = "当日详细数据", required = true)
    private List<SportDetails> listSportDetails;


    public long getDeviceId() {
        return this.deviceId;
    }

    public void setDeviceId(long deviceId) {
        this.deviceId = deviceId;
    }

    public String getTime() {
        return this.time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public int getTotalSteps() {
        return this.totalSteps;
    }

    public void setTotalSteps(int totalSteps) {
        this.totalSteps = totalSteps;
    }

    public int getTotalTime() {
        return this.totalTime;
    }

    public void setTotalTime(int totalTime) {
        this.totalTime = totalTime;
    }

    public int getTotalDistance() {
        return this.totalDistance;
    }

    public void setTotalDistance(int totalDistance) {
        this.totalDistance = totalDistance;
    }

    public int getTotalCalories() {
        return this.totalCalories;
    }

    public void setTotalCalories(int totalCalories) {
        this.totalCalories = totalCalories;
    }


    public void setListSportDetails(List<SportDetails> listSportDetails) {
        this.listSportDetails = listSportDetails;
    }


    public List<SportDetails> getListSportDetails() {
        return listSportDetails;
    }


    /**
     * Resets a to-many relationship, making the next get call to query for a fresh result.
     */
    @Override
    public String toString() {
        return "SportRecord{" +
                ", deviceId=" + deviceId +
                ", time='" + time + '\'' +
                ", totalSteps=" + totalSteps +
                ", totalTime=" + totalTime +
                ", totalDistance=" + totalDistance +
                ", totalCalories=" + totalCalories +
                ", listSportDetails=" + listSportDetails +
                '}';
    }

    public static Step toStep(SportRecord sportRecord) {
        Step step = BeanUtil.copyProperties(sportRecord, Step.class);
        List<SportDetails> listSportDetails = sportRecord.getListSportDetails();
        if (listSportDetails != null && listSportDetails.size() > 0) {
            step.setStepDetails(JSONObject.toJSONString(listSportDetails));
        }
        return step;
    }

    public static SportRecord toThis(Step step) {
        if (step == null) return null;
        SportRecord sportRecord = BeanUtil.copyProperties(step, SportRecord.class);
        if(step.getStepDetails()!=null){
            List<SportDetails> sportDetails = JSONArray.parseArray(step.getStepDetails(), SportDetails.class);
            sportRecord.setListSportDetails(sportDetails);
        }
        if (step.getTime() != null) {
            sportRecord.setTime(DateUtils.format(step.getTime(), DateUtils.dateFormat5));
        }
        return sportRecord;
    }

}
